package main;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import techs.*;


/**
 * @author 12341015
 * base for all other tech stuff used
 */
public class TechTree {
	int warTechLevel=1;
	int econTechLevel=1;
	double resef=1;
	Tech currentTech=null;
	Hashtable<String, Tech> TechSet;
	Set<String> validTechs;
	JFrame f = new JFrame("ALERT ALERT ALERT");

/**
 * base constructor
 * @param o Player Owner
 */
	public TechTree(Player o){//this method must be hard coded for our system to be functional. Ugly, but unavoidable.
		TechSet= new Hashtable<String,Tech>();

		//War Techs
		Tech War2= new WarTechLevel2(o);
		Tech War3= new WarTechLevel3(o);
		Tech MetalW= new MetalWorking(o);
		Tech MetalW2=new MetalWorking2(o);
		Tech MetalW3=new MetalWorking3(o);
		Tech Tanks= new BuildTanks(o);
		Tech Can=new BuildCannons(o);
		Can.setExplanation("Cannons");
		Tech Horse= new HorseBreeding(o);
		Horse.setExplanation("neigh neigh");
		Tech Armor1= new BasicArmor(o);
		Tech Armor2= new Tier2Armor(o);
		Tech Armor3= new Tier3Armor(o);
		TechSet.put("Armor2", Armor2);
		TechSet.put("Armor3", Armor3);
		TechSet.put("Can", Can);
		TechSet.put("Armor1", Armor1);
		TechSet.put("War2", War2);
		TechSet.put("War3", War3);
		TechSet.put("MetalW", MetalW);
		TechSet.put("MetalW2", MetalW2);
		TechSet.put("MetalW3", MetalW3);
		TechSet.put("Tanks", Tanks);
		TechSet.put("Horse", Horse);

		//Econ Techs (Will add spies later)
		Tech Econ2=new EconTechLevel2(o);
		Tech Econ3=new EconTechLevel3(o);
		Tech Resef1= new Resef1(o);
		Tech Resef2= new Resef2(o);
		Tech Resef3= new Resef3(o);
		Tech FoodBonus1= new FoodBonus1(o);
		Tech FoodBonus2= new FoodBonus2(o);
		Tech StoneBonus1= new StoneBonus1(o);
		Tech StoneBonus2= new StoneBonus2(o);
		Tech WoodBonus1= new WoodBonus1(o);
		Tech WoodBonus2= new WoodBonus2(o);
		Tech Points= new CultureSpread(o);
		Points.setExplanation("Culture");

		Resef1.setExplanation("Resource Efficiency lvl 1"); 

		TechSet.put("Resef1", Resef1);
		TechSet.put("Resef2", Resef2);
		TechSet.put("Resef3", Resef3);
		TechSet.put("Econ2", Econ2);
		TechSet.put("Econ3", Econ3);
		TechSet.put("FoodBonus1", FoodBonus1);
		TechSet.put("FoodBonus2", FoodBonus2);
		TechSet.put("StoneBonus1", StoneBonus1);
		TechSet.put("StoneBonus2", StoneBonus2);
		TechSet.put("WoodBonus1", WoodBonus1);
		TechSet.put("WoodBonus2", WoodBonus2);
		TechSet.put("Culture", Points);
	}


	/**
	 * does the check for if target can be researched and does switch if possible
	 * @param target
	 * @return possible or not, true false
	 */
	public boolean selectTech(String targetS){
		Tech target=null;
		if(TechSet.containsKey(targetS))
			target=TechSet.get(targetS);
		else {
			return false;
		}
		if(target.isComplete())
			return false;
		if(target.getTechtype()==1) {
			if(target.getTechlevel()<=warTechLevel){
				currentTech=target;
				return true;
			}
		}
		if(target.getTechtype()==2) {
			if(target.getTechlevel()<=econTechLevel){
				currentTech=target;
				return true;
			}
		}
		//System.err.println("not an error either [tech levels]");
		return false;
	}

/**
 * increment turn method.
 * @param Nresef New resource effeciency, passed by other methods
 * @param res new res, see above
 */
	public void IncrementTurn(double Nresef, int res){
		resef=Nresef;
		if(currentTech!=null)
			if(currentTech.IncrementTurn(resef*res))
				JOptionPane.showMessageDialog(f, currentTech.Name +" research complete. Choose another technology.");
	}
	/**
	 * @return the warTechLevel
	 */
	public int getWarTechLevel() {
		return warTechLevel;
	}
	/**
	 * @param warTechLevel the warTechLevel to set
	 */
	public void setWarTechLevel(int warTechLevel) {
		this.warTechLevel = warTechLevel;
	}
	/**
	 * @return the econTechLevel
	 */
	public int getEconTechLevel() {
		return econTechLevel;
	}
	/**
	 * @param econTechLevel the econTechLevel to set
	 */
	public void setEconTechLevel(int econTechLevel) {
		this.econTechLevel = econTechLevel;
	}
	/**
	 * @return the resef
	 */
	public double getResef() {
		return resef;
	}
	/**
	 * @param resef the resef to set
	 */
	public void setResef(double resef) {
		this.resef = resef;
	}
	public Set<String> getKeySet(){
		return TechSet.keySet();
	}
	/**
	 * check if a tech is complete in the proper panel
	 * @param tech tech to check
	 * @return complete?
	 */
	public boolean checkIfComplete(String tech){
		//System.out.println(TechSet.get(tech));
		return TechSet.get(tech).isComplete();
	}
}